package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.wuye.entity.WyFeeItem;

@Service
public class WyFeeItemService extends BaseService<WyFeeItem, String> {

	
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> getFeeItemData(String companyid, LoginUser loginUser){
		List<Map<String, String>> list = new ArrayList<>();
		if(loginUser == null){
			return list;
		}
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		String hql = "select new com.qingxin.wuye.entity.WyFeeItem(f.itemid, f.prjcode, f.prjname)" +
				" from WyFeeItem order by f.prjname";
		List<WyFeeItem> itemlist = listByHql(hql);
		if(itemlist != null){
			for(WyFeeItem item : itemlist){
				Map<String, String> map = new HashMap<>();
				map.put("itemid", item.getItemid());
				map.put("prjcode", item.getPrjcode());
				map.put("prjname", item.getPrjname());
				
				list.add(map);
			}
		}
		return list;
	}
	
	
	public Pager list(String companyid, PageParam pageParam, LoginUser loginUser, HttpServletRequest request){
		Querier querier = Querier.create("from WyFeeItem where companyid=:companyid order by createTime desc");
		querier.setParam("companyid", companyid);
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param feeItem
	 * @param typeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyFeeItem feeItem, LoginUser loginUser, HttpServletRequest request){
		feeItem.setCreateTime(new Date());
		feeItem.setUpdateTime(new Date());
		feeItem.setCreateBy(loginUser.getLogin_name());
		feeItem.setUpdateBy(loginUser.getLogin_name());
		
		this.save(feeItem);
		
		return null;
	}
	
	
	/**
	 * 修改信息
	 * @param feeItem
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyFeeItem feeItem, LoginUser loginUser, HttpServletRequest request){
		WyFeeItem old = getSafeEntity(feeItem.getItemid());
		
		String fields = "prjcode,prjname,feeMode,chargeMode,feeType,cycle,cycleNum,cycleUnit,price," +
				"isfee,accuracy,printIndex,tiered,latePaymentsRate,latePaymentsDays,incrementMonths,incrementPercent,formulas";
		BeanUtil.setTargetFields(old, feeItem, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyFeeItem> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
	}
	
	
	/**
	 * 删除
	 * @param itemids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] itemids, LoginUser loginUser, HttpServletRequest request){
		for(String itemid : itemids){
			WyFeeItem feeItem = get(itemid);
			if(feeItem==null){
				continue;
			}
			this.delete(feeItem);
		}
		
		return null;
	}
	
	
	
}
